package svr

import (
	"gitee.com/yysf-xin/dproxy/pkg/db"
	"gitee.com/yysf-xin/dproxy/pkg/tools"
	"github.com/armon/go-socks5"
	"net"
)

type Socket5Server struct {
	DB            *db.Client
	Socket5       *socks5.Server
	Addr          string
	l             net.Listener
}

func (s *Socket5Server) String() string {
	return "SOCKET5"
}

func (s *Socket5Server) Start() (err error) {
	if s.l != nil {
		return
	}
	s.l, err = net.Listen("tcp", s.Addr)
	if err == nil {
		go func() {
			tools.Log.Println(s, "start")
			err := s.Socket5.Serve(s.l)
			if err != nil {
				tools.Log.Println(s, err)
			}
		}()
	}
	return
}

func (s *Socket5Server) Stop() {
	if s.l != nil {
		err := s.l.Close()
		if err != nil {
			tools.Log.Println(s, "closed", err)
		}
		s.l = nil
	}
}
